package com.gpjcjl.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DividerItemDecoration;

import com.base.lib.JsonUtil;
import com.base.lib.SharedHelper;
import com.base.lib.ToastUtil;
import com.gpjcjl.BaseActivity;
import com.gpjcjl.Config;
import com.gpjcjl.R;
import com.gpjcjl.adapter.PropertyAdapter;
import com.gpjcjl.entity.Property;
import com.yanzhenjie.recyclerview.OnItemClickListener;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 产权单位列表页面
 */
public class PropertyListActivity extends BaseActivity implements TextWatcher, OnItemClickListener {

    @BindView(R.id.editText)
    EditText editText;
    @BindView(R.id.listView)
    SwipeRecyclerView listView;

    private PropertyAdapter adapter;
    private PropertyViewModel viewModel;
    private MainViewModel mainViewModel;

    public static void startIntentForResult(Activity context, int requestCode) {
        Intent it = new Intent(context, PropertyListActivity.class);
        context.startActivityForResult(it, requestCode);
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_property_list;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewModel = new ViewModelProvider(this).get(PropertyViewModel.class);
        mainViewModel = new ViewModelProvider(this).get(MainViewModel.class);
        DividerItemDecoration divider = new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL);
        divider.setDrawable(Objects.requireNonNull(ContextCompat.getDrawable(mContext, R.drawable.item_divider1)));
        listView.addItemDecoration(divider);
        editText.addTextChangedListener(this);
        listView.setOnItemClickListener(this);

        initData();

    }

    private void initData() {
        String cache = SharedHelper.getInstance().getString(Config.CACHE_PROPERTY, null);
        List<Property> list = JsonUtil.fromJson(cache, List.class, Property.class);
        if (list == null) {
            list = new ArrayList<>();
        }
        listView.setAdapter(adapter = new PropertyAdapter(mContext, list));
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    @Override
    public void afterTextChanged(Editable s) {
        if (TextUtils.isEmpty(s.toString().trim())) {
            initData();
        } else {
            adapter.filter(s.toString().trim());
        }
    }

    @Override
    public void onItemClick(View view, int position) {
        Intent it = new Intent();
        it.putExtra("result", adapter.getItem(position).getProperty());
        setResult(RESULT_OK, it);
        finish();
    }

    @OnClick({R.id.tv_add})
    public void onViewClicked(View view) {
        if (view.getId() == R.id.tv_add) {
            showAddDialog();
        }
    }

    private void showAddDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        View dialogView = LayoutInflater.from(mContext).inflate(R.layout.dialog_add_company, null);
        EditText editText = dialogView.findViewById(R.id.editText);
        builder.setTitle("添加").setCancelable(false).setView(dialogView)
                .setNegativeButton("取消", null)
                .setPositiveButton("确定", (dialog, which) -> {
                    String text = editText.getText().toString();
                    if (TextUtils.isEmpty(text)) {
                        ToastUtil.showToast(mContext, "请输入产权单位!");
                    } else {
                        dialog.cancel();
                        addProperty(text);
                    }
                }).create().show();
    }

    private void addProperty(String text) {
        showLoading();
        viewModel.editProperty(text, null).observe(PropertyListActivity.this, result -> {
            if (result.isSuccess()) {
                mainViewModel.init().observe(PropertyListActivity.this, result1 -> {
                    dismissLoading();
                    initData();
                });
            } else {
                dismissLoading();
                ToastUtil.showToast(mContext, result.getMsg());
            }
        });
    }
}
